alias kc='kubectl'
alias ka='kubeadm'
alias sc="systemctl"

# 使用debian10:
function kube.install() {
    apt update
    apt -y install sudo gnupg2 curl apt-transport-https
    apt -y install vim git curl wget

    # sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
    # echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

    # 使用阿里云镜像:
    curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -

    echo "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

    apt update
    apt -y install kubelet kubeadm kubectl
    apt-mark hold kubelet kubeadm kubectl

    # 查看版本：
    # kubectl version --output=json
    # kubeadm version --output=json

    # 禁用SELinux:
    # sudo setenforce 0
    # sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

    # 关闭防火墙:

    # 关闭Swap:
    # swapoff -a
    # sed -i '/swap/s/^/#/' /etc/fstab

    # 关闭防火墙:
    # systemctl stop firewalld
    # systemctl disable firewalld

}

function downloadxxx() {
    old="k8s.gcr.io"
    new="registry.cn-hangzhou.aliyuncs.com/google_containers"

    declare -a images=($(kubeadm config images list))
    for image in "${images[@]}"; do
        mirror=${image//$old/$new}

        # coredns 需要特殊处理:
        if [[ $mirror == *"coredns/"* ]]; then
            mirror=${mirror//"coredns/"/""} # k8s.gcr.io/coredns/coredns:xxx => registry.cn-hangzhou.aliyuncs.com/google_containers/coredns/coredns:xxx
        fi

        docker pull $mirror
        docker tag $mirror $image
        docker rmi $mirror
    done
}

# debian10安装docker-ce:
install_docker_ce() {
    if which docker >/dev/null 2>&1; then
        echo "--- docker is installed ---"
        docker --version
        docker-compose --version
        return 0
    fi

    apt remove docker docker-engine docker.io containerd runc || true
    apt -y install sudo apt-transport-https ca-certificates curl gnupg-agent software-properties-common

    curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/debian/gpg | sudo apt-key add -
    echo "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/debian buster stable" | sudo tee -a /etc/apt/sources.list.d/docker_ce.list 1>/dev/null 2>&1

    apt update
    apt install -y docker-ce docker-compose
    docker --version
    docker-compose --version
    systemctl start docker
    systemctl enable docker

    cat <<EOF >/etc/docker/daemon.json
{
    "registry-mirrors": [
        "https://docker.mirrors.ustc.edu.cn",
        "https://1nj0zren.mirror.aliyuncs.com"
    ]
}
EOF

}
